#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int N = 110, M = 210 * 2;
using pii = pair<int, int>;
int h[N], e[M], ne[M], w[M], idx;
int dist[N];
bool st[N];
int n, m;
void add(int a, int b, int c)
{
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
void dijkstra()
{
    priority_queue<pii, vector<pii>, greater<pii>> q;
    q.push({ 0,1 });
    dist[1] = 0;
    while (q.size())
    {
        auto t = q.top();
        q.pop();
        int ver = t.second;
        if (st[ver]) continue;
        for (int i = h[ver]; i != -1; i = ne[i])
        {
            int j = e[i];
            if (dist[j] > dist[ver] + w[i])
            {
                dist[j] = dist[ver] + w[i];
                q.push({ dist[j],j });
            }
        }
        st[ver] = true;
    }
}
int main()
{
    cin >> n >> m;
    memset(dist, 0x3f, sizeof dist);
    memset(h, -1, sizeof h);
    for (int i = 0; i < m; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        add(a, b, c), add(b, a, c);
    }
    dijkstra();
    int res = 0;
    for (int i = 2; i <= n; i++) res = max(res, dist[i]);
    if (res == 0x3f3f3f3f) res = -1;
    cout << res << endl;
    return 0;
}